package com.grapevine.contact.util;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.text.TextUtils;
import android.util.Log;
import com.grapevine.contact.data.ContactData;
import com.grapevine.contact.data.ItemData;
import com.grapevine.contact.db.TypeManager;

public class AndroidContact implements TypeManager
{

	private static String TAG = AndroidContact.class.getSimpleName();

	public static ContactData getContacts(Context c, long id)
	{

		Cursor cur = c.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
				Contacts._ID + " = " + id, null, null);

		ContactData data = new ContactData();

		if (cur.moveToFirst())
		{

			String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
			data.setPhoneId(id);
			data.setName(name);

			// Phone
			Cursor phones = c.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
					ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);

			if (phones.moveToFirst())
			{
				do
				{
					ItemData itemData = new ItemData();
					String number = phones.getString(phones
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
					if (TextUtils.isEmpty(number))
						continue;
					itemData.setFatherType(PHONE);
					itemData.setUuid(Util.createNewUUID());
					itemData.setLastmodified(phones.getString(phones
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LAST_TIME_CONTACTED)));
					itemData.setValue(number);
					int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

					if (ContactsContract.CommonDataKinds.Phone.TYPE_WORK == type)
					{
						Log.e(TAG, "TYPE_WORK");
						itemData.setType(PHONE_TYPE_WORK);
						itemData.setLable("工作电话");
					}
					else if (ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE == type)
					{
						Log.e(TAG, "TYPE_MOBILE");
						itemData.setType(PHONE_TYPE_MOBILE);
						itemData.setLable("手机号码");
					}
					else if (ContactsContract.CommonDataKinds.Phone.TYPE_HOME == type)
					{
						Log.e(TAG, "TYPE_HOME");
						itemData.setType(PHONE_TYPE_HOME);
						itemData.setLable("住址号码");
					}
					else if (ContactsContract.CommonDataKinds.Phone.TYPE_OTHER == type)
					{
						itemData.setType(PHONE_TYPE_OTHER);
						itemData.setLable("其它号码");
					}
					else if (ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM == type)
					{
						itemData.setType(PHONE_TYPE_CUSTOM);
						itemData.setLable(phones.getString(phones
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL)));
					}
					data.getItemDatas().add(itemData);
				}
				while (phones.moveToNext());
			}
			phones.close();
		}
		cur.close();
		return data;
	}

	public static List<ContactData> getContacts(Context c)
	{

		List<ContactData> list = new ArrayList<ContactData>();
		//
		// String groupWhere = ContactsContract.Data.MIMETYPE + " = ?";
		//
		// String[] groupArgs = new String[] {
		// ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE };
		//
		// Cursor groupCursor =
		// c.getContentResolver().query(ContactsContract.Groups.CONTENT_URI,
		// null, null,
		// null, null);
		//
		//		
		// if (groupCursor.moveToFirst())
		// {
		// String []groupNames = groupCursor.getColumnNames();
		//			
		// do
		// {
		// // String name =
		// groupCursor.getString(groupCursor.getColumnIndexOrThrow("name"));
		// // Log.e(TAG, "name = " + name);
		//				
		//				
		// for(String n:groupNames){
		// String lable = groupCursor.getString(groupCursor.getColumnIndex(n));
		//					
		// Log.e(TAG, n + " = " + lable);
		// }
		//				
		//				
		// }
		// while (groupCursor.moveToNext());
		// }
		// groupCursor.close();
		//
		//		
		// ContactsContract.Contacts.
		//		
		// return list;

		// Cursor cur =
		// c.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
		// null, null, null, null);

		Cursor cur = c.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
				Contacts.HAS_PHONE_NUMBER + " = '1'", null,
				ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

		if (cur.moveToFirst())
		{

			do
			{

				ContactData data = new ContactData();
				int id = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts._ID));
				// String name =
				// cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
				// int index = name.indexOf(' ');
				// if (-1 != index)
				// {
				// String names[] = name.split(" ");
				// name = names[1] + names[0];
				// }

				String name = null;

				String structWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE
						+ " = ?";
				String[] structWhereParams = new String[] { String.valueOf(id),
						ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };

				Cursor structCur = c.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, structWhere,
						structWhereParams, null);

				// firstname = givename
				if (structCur.moveToFirst())
				{
					String given_name = structCur.getString(structCur
							.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
					String family_name = structCur.getString(structCur
							.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));

					String middle = structCur.getString(structCur
							.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));

					name = null == family_name ? "" : family_name;

					name += null == middle ? "" : middle;

					name += null == given_name ? "" : given_name;

				}
				structCur.close();

				Log.e(TAG, "id = " + id);
				Log.e(TAG, "name = " + name);

				if (TextUtils.isEmpty(name))
					continue;

				String uuid = Util.createNewUUID();
				data.setUuid(uuid);
				data.setPhoneId(id);
				data.setName(name);
				data.setLastModified(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LAST_TIME_CONTACTED)));
				Log.e(TAG, "id = " + id);
				Log.e(TAG, "name = " + name);

				// Phone
				Cursor phones = c.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
						ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);

				if (phones.moveToFirst())
				{
					do
					{
						ItemData itemData = new ItemData();
						String number = phones.getString(phones
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
						if (TextUtils.isEmpty(number))
							continue;
						Log.e(TAG, "number = " + number);
						itemData.setFatherType(PHONE);
						itemData.setPUuid(uuid);
						itemData.setUuid(Util.createNewUUID());
						itemData.setLastmodified(phones.getString(phones
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LAST_TIME_CONTACTED)));
						itemData.setValue(number);
						int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

						if (ContactsContract.CommonDataKinds.Phone.TYPE_WORK == type)
						{
							Log.e(TAG, "TYPE_WORK");
							itemData.setType(PHONE_TYPE_WORK);
							itemData.setLable("工作电话");
						}
						else if (ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE == type)
						{
							Log.e(TAG, "TYPE_MOBILE");
							itemData.setType(PHONE_TYPE_MOBILE);
							itemData.setLable("手机号码");
						}
						else if (ContactsContract.CommonDataKinds.Phone.TYPE_HOME == type)
						{
							Log.e(TAG, "TYPE_HOME");
							itemData.setType(PHONE_TYPE_HOME);
							itemData.setLable("住址号码");
						}
						else if (ContactsContract.CommonDataKinds.Phone.TYPE_OTHER == type)
						{
							itemData.setType(PHONE_TYPE_OTHER);
							itemData.setLable("其它号码");
						}
						else if (ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM == type)
						{
							itemData.setType(PHONE_TYPE_CUSTOM);
							itemData.setLable(phones.getString(phones
									.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL)));
						}
						data.getItemDatas().add(itemData);
					}
					while (phones.moveToNext());
				}
				phones.close();

				Log.e(TAG, "===============");

				// Email
				Cursor emailCursor = c.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
						null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + id, null, null);

				if (emailCursor.moveToFirst())
				{
					do
					{
						ItemData itemData = new ItemData();
						String email = emailCursor.getString(emailCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
						Log.e(TAG, "email = " + email);
						if (TextUtils.isEmpty(email))
							continue;
						itemData.setPUuid(uuid);
						itemData.setUuid(Util.createNewUUID());
						itemData.setFatherType(EMAIL);
						itemData.setValue(email);
						itemData.setLastmodified(emailCursor.getString(emailCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Email.LAST_TIME_CONTACTED)));
						int type = emailCursor.getInt(emailCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

						switch (type)
						{
						case ContactsContract.CommonDataKinds.Email.TYPE_HOME:
							Log.e(TAG, "TYPE_HOME");
							itemData.setType(EMAIL_TYPE_HOME);
							itemData.setLable("家庭邮箱");
							break;

						case ContactsContract.CommonDataKinds.Email.TYPE_WORK:
							Log.e(TAG, "TYPE_WORK");
							itemData.setType(EMAIL_TYPE_WORK);
							itemData.setLable("工作邮箱");
							break;

						case ContactsContract.CommonDataKinds.Email.TYPE_MOBILE:
							Log.e(TAG, "TYPE_MOBILE");
							itemData.setType(EMAIL_TYPE_MOBILE);
							itemData.setLable("手机邮箱");
							break;
						case ContactsContract.CommonDataKinds.Email.TYPE_OTHER:
							itemData.setType(EMAIL_TYPE_OTHER);
							itemData.setLable("其他邮箱");
							break;
						case ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM:
							String lable = emailCursor.getString(emailCursor
									.getColumnIndex(ContactsContract.CommonDataKinds.Email.LABEL));
							itemData.setType(EMAIL_TYPE_CUSTOM);
							itemData.setLable(lable);
							Log.e(TAG, "lable = " + lable);
							break;

						default:
							break;
						}
						data.getItemDatas().add(itemData);
					}
					while (emailCursor.moveToNext());
				}
				emailCursor.close();

				Log.e(TAG, "===============");

				// address

				String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE
						+ " = ?";
				String[] addrWhereParams = new String[] { String.valueOf(id),
						ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };

				Cursor addrCur = c.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, addrWhere,
						addrWhereParams, null);
				if (addrCur.moveToFirst())
				{

					do
					{
						ItemData itemData = new ItemData();
						itemData.setFatherType(ADDRESS);
						itemData.setUuid(Util.createNewUUID());
						itemData.setPUuid(uuid);
						itemData
								.setLastmodified(addrCur
										.getString(addrCur
												.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.LAST_TIME_CONTACTED)));
						JSONObject jo = new JSONObject();
						String poBox = addrCur.getString(addrCur
								.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
						Log.e(TAG, "poBox = " + poBox);
						put(jo, "pobox", poBox);
						String street = addrCur.getString(addrCur
								.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
						Log.e(TAG, "street = " + street);
						put(jo, "street", street);
						String city = addrCur.getString(addrCur
								.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
						Log.e(TAG, "city = " + city);
						put(jo, "city", city);
						String state = addrCur.getString(addrCur
								.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
						Log.e(TAG, "state = " + state);
						put(jo, "region", state);
						String postalCode = addrCur.getString(addrCur
								.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
						Log.e(TAG, "postalCode = " + postalCode);
						put(jo, "postcode", postalCode);
						String country = addrCur.getString(addrCur
								.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
						Log.e(TAG, "country = " + country);
						put(jo, "country", country);

						itemData.setValue(jo.toString());

						int type = addrCur.getInt(addrCur
								.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));

						if (ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME == type)
						{
							itemData.setType(ADDRESS_TYPE_HOME);
							itemData.setLable("住宅");

						}
						else if (ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK == type)
						{
							itemData.setType(ADDRESS_TYPE_WORK);
							itemData.setLable("单位");

						}
						else if (ContactsContract.CommonDataKinds.StructuredPostal.TYPE_OTHER == type)
						{
							itemData.setType(ADDRESS_TYPE_OTHER);
							itemData.setLable("其他");

						}
						else if (ContactsContract.CommonDataKinds.StructuredPostal.TYPE_CUSTOM == type)
						{
							Log.e(TAG, "custom");
							String lable = addrCur.getString(addrCur
									.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.LABEL));
							Log.e(TAG, "lable = " + lable);
							itemData.setType(ADDRESS_TYPE_CUSTOM);
							itemData.setLable(lable);

						}
						data.getItemDatas().add(itemData);
					}
					while (addrCur.moveToNext());
				}
				addrCur.close();
				Log.e(TAG, "================");
				// IM
				String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";

				String[] argsWhere = new String[] { String.valueOf(id),
						ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE };
				Cursor im = c.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, argsWhere,
						null);

				if (im.moveToFirst())
				{
					do
					{

						String num = im.getString(im.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
						Log.e(TAG, "num = " + num);
						if (TextUtils.isEmpty(num))
							continue;

						ItemData itemData = new ItemData();
						itemData.setPUuid(uuid);
						itemData.setUuid(Util.createNewUUID());
						itemData.setType(IM);
						itemData.setLastmodified(im.getString(im
								.getColumnIndex(ContactsContract.CommonDataKinds.Im.LAST_TIME_CONTACTED)));
						itemData.setValue(num);
						int type = im.getInt(im.getColumnIndex(ContactsContract.CommonDataKinds.Im.PROTOCOL));

						switch (type)
						{
						case ContactsContract.CommonDataKinds.Im.PROTOCOL_AIM:
							itemData.setType(IM_TYPE_AIM);
							itemData.setLable("AIM");
							break;
						case ContactsContract.CommonDataKinds.Im.PROTOCOL_QQ:
							itemData.setType(IM_TYPE_QQ);
							itemData.setLable("QQ");
							break;
						case ContactsContract.CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK:
							itemData.setLable("Google Talk");
							itemData.setType(IM_TYPE_GOOGLE_TALK);

						case ContactsContract.CommonDataKinds.Im.PROTOCOL_MSN:
							itemData.setLable("Window Live");
							itemData.setType(IM_TYPE_MSN);
							break;
						case ContactsContract.CommonDataKinds.Im.PROTOCOL_SKYPE:
							itemData.setLable("Skype");
							itemData.setType(IM_TYPE_SKYPE);
							break;
						case ContactsContract.CommonDataKinds.Im.PROTOCOL_YAHOO:
							itemData.setType(IM_TYPE_YAHOO);
							itemData.setLable("Yahoo");
							break;

						case ContactsContract.CommonDataKinds.Im.PROTOCOL_CUSTOM:
							Log.e(TAG, "CUSTOM");
							String lable = im.getString(im
									.getColumnIndex(ContactsContract.CommonDataKinds.Im.CUSTOM_PROTOCOL));
							Log.e(TAG, "lable = " + lable);
							itemData.setType(IM_TYPE_CUSTOM);
							itemData.setLable(lable);
							break;
						case ContactsContract.CommonDataKinds.Im.PROTOCOL_ICQ:
							itemData.setLable("ICQ");
							itemData.setType(IM_TYPE_ICQ);
							break;
						default:
							break;
						}
					}
					while (im.moveToNext());
				}
				im.close();
				Log.e(TAG, "===========");

				// ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE;

				// NICKNAME
				String wheres = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE
						+ " = ?";
				String[] argsWheres = new String[] { String.valueOf(id),
						ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE };
				Cursor nick = c.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, wheres, argsWheres,
						null);
				if (nick.moveToFirst())
				{
					do
					{

						String nickName = nick.getString(nick
								.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME));
						if (TextUtils.isEmpty(nickName))
							continue;
						ItemData itemData = new ItemData();
						itemData.setPUuid(uuid);
						itemData.setUuid(Util.createNewUUID());
						itemData.setLable("昵称");

						itemData.setFatherType(NICKNAME);
						itemData.setValue(nickName);
						itemData.setLastmodified(nick.getString(nick
								.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.LAST_TIME_CONTACTED)));
						Log.e(TAG, "nickName = " + nickName);
						data.getItemDatas().add(itemData);
					}
					while (nick.moveToNext());
				}
				nick.close();
				Log.e(TAG, "==================");

				// website
				String wherew = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE
						+ " = ?";
				String[] argsWherew = new String[] { String.valueOf(id),
						ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE };
				Cursor website = c.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, wherew,
						argsWherew, null);
				if (website.moveToFirst())
				{
					do
					{
						String url = website.getString(website
								.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL));
						Log.e(TAG, "url = " + url);
						if (TextUtils.isEmpty(url))
							continue;

						ItemData itemData = new ItemData();
						itemData.setPUuid(uuid);
						itemData.setUuid(Util.createNewUUID());
						itemData.setLable("网站");
						itemData.setFatherType(WEBSITE);

						itemData.setValue(url);
						itemData.setLastmodified(website.getString(website
								.getColumnIndex(ContactsContract.CommonDataKinds.Website.LAST_TIME_CONTACTED)));
						data.getItemDatas().add(itemData);
					}
					while (website.moveToNext());
				}
				website.close();
				Log.e(TAG, "============");
				// Organization
				String whereo = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE
						+ " = ?";
				String[] argsWhereo = new String[] { String.valueOf(id),
						ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE };
				Cursor orign = c.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereo,
						argsWhereo, null);
				if (orign.moveToFirst())
				{
					do
					{
						ItemData itemData = new ItemData();
						itemData.setPUuid(uuid);
						itemData.setUuid(Util.createNewUUID());
						itemData.setFatherType(ORGANIZATION);
						itemData.setLastmodified(orign.getString(orign
								.getColumnIndex(ContactsContract.CommonDataKinds.Organization.LAST_TIME_CONTACTED)));
						JSONObject jo = new JSONObject();
						String company = orign.getString(orign
								.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));
						Log.e(TAG, "公司 = " + company);
						put(jo, "company", company);
						String job = orign.getString(orign
								.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
						Log.e(TAG, "工作 = " + job);
						put(jo, "job", job);
						itemData.setValue(jo.toString());
						int type = orign.getInt(orign
								.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TYPE));
						Log.e(TAG, "type = " + type);
						switch (type)
						{
						case ContactsContract.CommonDataKinds.Organization.TYPE_WORK:

							Log.e(TAG, "公司");
							itemData.setLable("公司");
							itemData.setType(ORGANIZATION_TYPE_COMPANY);
							break;
						case ContactsContract.CommonDataKinds.Organization.TYPE_OTHER:
							itemData.setType(ORGANIZATION_TYPE_OTHER);
							itemData.setLable("其他");
							Log.e(TAG, "其他");
							break;
						case ContactsContract.CommonDataKinds.Organization.TYPE_CUSTOM:
							Log.e(TAG, "自定义");
							itemData.setType(ORGANIZATION_TYPE_CUSTOM);
							itemData.setLable(orign.getString(orign
									.getColumnIndex(ContactsContract.CommonDataKinds.Organization.LABEL)));
							break;

						default:
							break;
						}
						data.getItemDatas().add(itemData);
					}
					while (orign.moveToNext());
				}
				orign.close();

				String wheren = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE
						+ " = ?";
				String[] argsWheren = new String[] { String.valueOf(id),
						ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE };
				Cursor noteCursor = c.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, wheren,
						argsWheren, null);

				if (noteCursor.moveToFirst())
				{
					do
					{
						String value = noteCursor.getString(noteCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
						if (TextUtils.isEmpty(value))
							continue;
						ItemData itemData = new ItemData();
						itemData.setPUuid(uuid);
						itemData.setUuid(Util.createNewUUID());
						itemData.setFatherType(COMMENT);
						itemData.setLable("备注");
						itemData.setLastmodified(noteCursor.getString(noteCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Note.LAST_TIME_CONTACTED)));

						itemData.setValue(value);
						Log.e(TAG, "value = " + value);

						data.getItemDatas().add(itemData);
					}
					while (noteCursor.moveToNext());
				}
				noteCursor.close();

				String str = JSONUtil.createJson(data).toString();

				Log.e(TAG, "src = " + str);
				list.add(data);
			}
			while (cur.moveToNext());
		}
		cur.close();
		return list;
	}

	private static void put(JSONObject jo, String key, Object o)
	{
		try
		{
			jo.put(key, o);
		}
		catch (JSONException e)
		{
			e.printStackTrace();
		}
	}

}